﻿<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>孙悟空^_^</title>
	<meta charset="UTF-8">
	<style>
		html, body
		{
			border: 0;
			margin: 0;
			height: 100%;
			height: 100%;
		}
	</style>

	<script id="shader-fs" type="shader/fragment">
		#ifdef GL_ES
		precision highp float;
		#endif

		void main(void)
		{
				gl_FragColor = vec4(1.0, 1.0, 0.01, 1.0);
		}
	</script>

	<script id="shader-vs" type="shader/vertex">
		attribute vec3 aVertexPosition;

		void main(void)
		{
				gl_Position = vec4(aVertexPosition, 1.0);
		}
	</script>

	<script type="text/javascript">

		var gl;
		function initGL(canvas)
		{
			try {
				canvas.width = window.innerWidth;
				canvas.height = window.innerHeight;
				//gl = canvas.getContext("experimental-webgl", { antialias: false });
				gl = canvas.getContext("experimental-webgl", { antialias: true });
				gl.viewportWidth = canvas.width;
				gl.viewportHeight = canvas.height;
			} catch (e) {}

			if (!gl)
			{
				alert("Could not initialise WebGL, sorry :-(");
			}
		}


		function getShader(gl, id)
		{
			var shaderScript = document.getElementById(id);
			if (!shaderScript)
			{
				return null;
			}

			var str = "";
			var k = shaderScript.firstChild;
			while (k)
			{
				if (k.nodeType == 3)
				{
					str += k.textContent;
				}
				k = k.nextSibling;
			}

			var shader;
			if (shaderScript.type == "shader/fragment")
			{
				shader = gl.createShader(gl.FRAGMENT_SHADER);
			}
			else if (shaderScript.type == "shader/vertex")
			{
				shader = gl.createShader(gl.VERTEX_SHADER);
			}
			else
			{
				return null;
			}

			gl.shaderSource(shader, str);
			gl.compileShader(shader);

			if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
			{
				alert(gl.getShaderInfoLog(shader));
				return null;
			}

			return shader;
		}


		var shaderProgram;

		function initShaders()
		{
			var fragmentShader = getShader(gl, "shader-fs");
			var vertexShader = getShader(gl, "shader-vs");

			shaderProgram = gl.createProgram();
			gl.attachShader(shaderProgram, vertexShader);
			gl.attachShader(shaderProgram, fragmentShader);
			gl.linkProgram(shaderProgram);

			if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS))
			{
				alert("Could not initialise shaders");
			}

			gl.useProgram(shaderProgram);

			shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
			gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

		}

		var triangleVertexPositionBuffer;

		function initBuffers()
		{
			triangleVertexPositionBuffer = gl.createBuffer();
			gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
			var vertices = [ 0.0,  0.5, 0.0,
					-0.5, -0.5, 0.0,
					 0.5, -0.5, 0.0 ];

			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
			triangleVertexPositionBuffer.itemSize = 3;
			triangleVertexPositionBuffer.numItems = 3;
		}


		function drawScene()
		{
			gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
			gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

			gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
			gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
			gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
		}


		function main()
		{
			var canvas = document.getElementById("webgl-canvas");
			initGL(canvas);
			initShaders();
			initBuffers();

			gl.clearColor(1.0, 0.0, 0.0, 1.0);
			drawScene();
		}

		window.onresize = function ()
		{
			gl.canvas.width = window.innerWidth;
			gl.canvas.height = window.innerHeight;
			gl.viewportWidth = gl.canvas.width;
			gl.viewportHeight = gl.canvas.height;
			gl.clearColor(1.0, 0.0, 0.0, 1.0);
			drawScene();
		}


</script>
</head>
<body onload="main();">
	<canvas id="webgl-canvas" style="width: 100%; height: 100%; cursor: auto;" width="1680", height="486"></canvas>
</body>
</html>
